<template>
  <div class="message-box-container">
    <div class="message-box" ref="messageBox">
      <div class="box-title">
        <el-progress type="circle" :percentage="progress" :width="80" :show-text="false" status="warning"></el-progress>
        <div class="duration">{{second}}</div>
      </div>
      <div class="box-content">{{content}}</div>
      <div class="message-box-footer">
        <div class="message-box-btn" v-if="showCancel" @click="cancelHandler">{{cancelText}}</div>
        <div class="message-box-btn message-box-primary" @click="confirmHandler">{{confirmText}}</div>
      </div>
    </div>
  </div>
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'

@Component
export default class MessageBox extends Vue {
  private confirmText = '确定'
  private cancelText = '取消'
  private duration = 30
  private showCancel = true
  private confirm ?: {():void}
  private cancel ?: {():void}
  private title = '提示'
  private content = ''
  private handlers : number[] = []
  private second = 0

  private created () {
    this.second = this.duration
    this.countDown()
  }

  get progress () : number {
    const progress = Math.ceil(this.second / this.duration * 100)
    return progress
  }

  private countDown () {
    const handler = setTimeout(() => {
      if (this.second > 0) {
        this.second--
        this.countDown()
      } else {
        this.cancelHandler()
      }
    }, 1000)
    this.handlers.push(handler)
  }

  private clear () {
    this.handlers.forEach(element => {
      clearTimeout(element)
    })
  }

  // 确定事件
  public async confirmHandler () : Promise<void> {
    if (this.confirm !== undefined) {
      await this.confirm()
    }
    this.clear()
    const element = this.$el.parentNode
    if (element !== null) {
      element.removeChild(this.$el)
    }
  }

  // 取消事件
  public async cancelHandler () : Promise<void> {
    if (this.cancel !== undefined) {
      await this.cancel()
    }
    this.clear()
    const element = this.$el.parentNode
    if (element !== null) {
      element.removeChild(this.$el)
    }
  }
}
</script>

<style scoped>
  .message-box-container{
    position: fixed;
    top: 1110px;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: rgba(0, 0, 0, 0.4);
    z-index: 99;
    display: flex;
    justify-content: center;
    align-items:center;
  }

  .message-box{
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    align-items: center;
    width: 50rem;
    height: 30rem;
    background: #F8F8F8;
    border-radius: 10px;
    font-size: 3rem;
    box-shadow: 0px 2px 10px 0px rgba(0, 0, 0, 0.05);
    padding: 2rem;
  }

  .message-box-footer{
    display: flex;
    justify-content: space-around;
    align-items: center;
    width: 80%;
  }

  .message-box-btn{
    width: 10rem;
    height: 4rem;
    font-size: 2.5rem;
    line-height: 4rem;
    border-radius: 4rem;
    padding: 1rem;
    margin: 0 auto;
    text-align: center;
    background-color: #D9D9D9;
    color: #444444;
  }

  .message-box-primary{
    background-color: #01D8A0;
    color: white;
  }

  .box-title{
    display: flex;
    justify-content: center;
    align-content: center;
    align-items: center;
    position: relative;
  }

  .duration{
    position: absolute;
    color: #444444;
  }
</style>
